home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / src / fig14_08.jar / Ch14 / Fig14_08 / Fig14_08.cpp < prev    next >
C/C++ Source or Header  |  1997-11-04  |  3KB  |  102 lines

  1. // Fig. 14.8: fig14_08.cpp
  2. // Credit inquiry program
  3. #include <iostream.h>
  4. #include <fstream.h>
  5. #include <iomanip.h>
  6. #include <stdlib.h> 
  7.  
  8. enum RequestType { ZERO_BALANCE = 1, CREDIT_BALANCE, 
  9.                    DEBIT_BALANCE, END };
  10. int getRequest();
  11. bool shouldDisplay( int, double );
  12. void outputLine( int, const char *, double );
  13.  
  14. int main()
  15. {
  16.    // ifstream constructor opens the file
  17.    ifstream inClientFile( "clients.dat", ios::in );
  18.  
  19.    if ( !inClientFile ) {
  20.       cerr << "File could not be opened" << endl;
  21.       exit( 1 );
  22.    }
  23.  
  24.    int request;
  25.    int account;
  26.    char name[ 30 ];
  27.    double balance;
  28.  
  29.    cout << "Enter request\n"
  30.         << " 1 - List accounts with zero balances\n"
  31.         << " 2 - List accounts with credit balances\n"
  32.         << " 3 - List accounts with debit balances\n" 
  33.         << " 4 - End of run";
  34.    request = getRequest();
  35.  
  36.    while ( request != END ) {
  37.  
  38.       switch ( request ) {
  39.          case ZERO_BALANCE:
  40.             cout << "\nAccounts with zero balances:\n";
  41.             break;
  42.          case CREDIT_BALANCE:
  43.             cout << "\nAccounts with credit balances:\n";
  44.             break;
  45.          case DEBIT_BALANCE:
  46.             cout << "\nAccounts with debit balances:\n";
  47.             break;
  48.       }
  49.  
  50.       inClientFile >> account >> name >> balance;
  51.  
  52.       while ( !inClientFile.eof() ) {
  53.          if ( shouldDisplay( request, balance ) )
  54.             outputLine( account, name, balance );
  55.  
  56.          inClientFile >> account >> name >> balance;
  57.       }
  58.       
  59.       inClientFile.clear();    // reset eof for next input
  60.       inClientFile.seekg( 0 ); // move to beginning of file
  61.       request = getRequest();
  62.    }
  63.  
  64.    cout << "End of run." << endl;
  65.  
  66.    return 0;   // ifstream destructor closes the file
  67. }
  68.  
  69. int getRequest()
  70.    int request;
  71.    
  72.    do {
  73.       cout << "\n? ";
  74.       cin >> request;
  75.    } while( request < ZERO_BALANCE && request > END );
  76.  
  77.    return request;
  78. }
  79.  
  80. bool shouldDisplay( int type, double balance )
  81. {
  82.    if ( type == CREDIT_BALANCE && balance < 0 )
  83.       return true;
  84.  
  85.    if ( type == DEBIT_BALANCE && balance > 0 )
  86.       return true;
  87.  
  88.    if ( type == ZERO_BALANCE && balance == 0 )
  89.       return true;
  90.  
  91.    return false;
  92. }
  93.  
  94. void outputLine( int acct, const char *name, double bal )
  95. {
  96.    cout << setiosflags( ios::left ) << setw( 10 ) << acct
  97.         << setw( 13 ) << name << setw( 7 ) << setprecision( 2 )
  98.         << resetiosflags( ios::left )
  99.         << setiosflags( ios::fixed | ios::showpoint )
  100.         << bal << '\n';
  101. }